<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/7/1
 * Time: 8:33 AM
 */
namespace app\common\models;

use app\common\exceptions\BizException;

class Category extends BaseModel
{
    public $table = 'yq_category';
    public $timestamps = true;
    protected $guarded = [''];

    const TYPE_ALL = -1;//全部类型
    const TYPE_ARTICLE = 0;//文章类型
    const TYPE_PRODUCT = 1;//产品类型

    public static $typeMap = [
        self::TYPE_ARTICLE=>'文章',
        self::TYPE_PRODUCT=>'产品',
    ];
    public function parent()
    {
        return $this->hasOne(self::class, 'id', 'pid');
    }
    public function children()
    {
        return $this->hasMany(self::class, "pid",'id');
    }
    public static function getById($id){
        return self::site()->select()
            ->where('id',$id)
            ->first();
    }

    /**
     *
     * @param $params
     * $params=[
     * 'select'=>字段1，字段2 或空
     * 'where'=>sql条件字符串,
     * 'order'=>sql排序字符串
     * 'limit'=>sql的limit,
     * 'page_size'=>数字
     * ]
     * @return Category
     * @throws BizException
     */
    public static function getListByCondition($params){
        $query = self::site();
        if($params['select']){
            $query = $query->select($params['select']);
        }else{
            $query = $query->select();
        }
        $query = $query->where('status',self::STATUS_NORMAL);
        if($params['where']){
            $query = $query->whereRaw($params['where']);
        }
        if($params['order']){
            $query = $query->orderByRaw($params['order']);
        }
        if($params['limit'] && $params['page_size']){
            throw new BizException('limit和page_size只能传其中一个');
        }
        if($params['limit']){
            $limit = explode(',',$params['limit']);
            if(count($limit)==2){
                $query = $query
                    ->offset(intval($limit[0]))
                    ->limit(intval($limit[1]));
            }else{
                $query = $query
                    ->limit(intval($params['limit']));
            }
        }
        if($params['page_size']){
            $query = $query
                ->paginate($params['page_size']);
        }else{
            $query = $query->get();
        }
        return $query;
    }
    /**
     *
     * @param $params
     * $params=[
     * 'select'=>字段1，字段2 或空
     * 'where'=>sql条件字符串,
     * 'order'=>sql排序字符串
     * ]
     * @return Category
     */
    public static function getFirstByCondition($params){
        $query = self::site();
        if($params['select']){
            $query = $query->select($params['select']);
        }else{
            $query = $query->select();
        }
        if($params['where']){
            $query = $query->whereRaw($params['where']);
        }
        if($params['order']){
            $query = $query->orderByRaw($params['order']);
        }
        $query = $query->first();
        return $query;
    }
    public  function atributeNames() {
        return [
            'name'  => '分类名称'
        ];
    }

    public  function rules()
    {
        return [
            'name' => 'required'
        ];
    }
}
